package com.uikoo9.manage.y.util;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: shoko (fbjia.com)
 * Date: 13-6-7
 * Time: 下午2:55
 * To change this template use File | Settings | File Templates.
 */
public class PostToPage {
    protected static final String lineSign = java.lang.System.getProperty(
            "line.separator");
    protected static final int lineSign_size = lineSign.length();


    /**
     * @param Post        context,link,linktext,keyword
     * @param _targetfile
     * @return
     */
    public static String toPage(Map<String, String> Post, String _targetfile) {
        String metakeywords = "<META content={0} name=keywords>";
        String metatitle = "<TITLE>{0}</TITLE>";
        String metadesc = "<META content={0} name=description>";
        String netshap = "<p> 正文快照: 时间{0}</p> ";
        String tempLeate = "<LI class=active><A href=\"{0}\" target=_blank>{1}</A></LI>";
        String crop = "<p><A href=\"{0}\" target=_blank>{1}</A></p> ";
        String _dd = "";
        StringBuffer testContext = new StringBuffer(Post.get("context"));

//        ActionContext ac = ActionContext.getContext();
//        HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
        HttpServletRequest request = null;
        java.lang.System.out.println("----------------" + request.getRealPath("/") + _targetfile);
        String destfilePath = request.getRealPath("/") + _targetfile;
        String srcfilePath = request.getRealPath("/") + "page\\context.vm";

        String path0 = destfilePath.substring(0, destfilePath.lastIndexOf("\\"));
        String path1 = destfilePath;
        java.io.File f1 = new java.io.File(path0);        // 创建文件夹
        if (!f1.exists()) {
            f1.mkdirs();
        }
        f1 = new java.io.File(path1);        // 创建文件
        if (!f1.exists()) {
            try {
                f1.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        String _link = Post.get("link");
        String _linktext = Post.get("linktext");
        String _keyword = Post.get("keyword");

        try {

            BufferedReader reader = new BufferedReader(new java.io.InputStreamReader(new FileInputStream(srcfilePath), "utf-8"));
            java.io.BufferedWriter writer = new java.io.BufferedWriter(new java.io.OutputStreamWriter(new java.io.FileOutputStream(destfilePath), "utf-8"));

            String lineContext = testContext.toString();

            String line;

            while ((line = reader.readLine()) != null) {
                int start = line.indexOf("#context");

                if (start >= 0) {
                    String tempCrop = StringUtil.replace(crop, "{0}",
                            _link);
                    tempCrop = StringUtil.replace(tempCrop, "{1}",
                            "      原文链接： " + _link);

                    writer.write(tempCrop + lineSign);
                    writer.write(netshap + lineSign);

                    writer.write(lineContext + lineSign);

                    continue;
                }

                int start1 = line.indexOf("#titledesc");

                if (start1 >= 0) {
                    String tempLine = StringUtil.replace(tempLeate,
                            "{0}", "test.htm");
                    tempLine = StringUtil.replace(tempLine, "{1}",
                            "标题:  " + _linktext);

                    writer.write(tempLine + lineSign);

                    continue;
                }

                int start2 = line.indexOf("#metatitle");

                if (start2 >= 0) {
                    metatitle = StringUtil.replace(metatitle, "{0}",
                            _linktext);

                    writer.write(metatitle + lineSign);

                    continue;
                }

                int start3 = line.indexOf("#metadesc");

                if (start3 >= 0) {
                    metadesc = StringUtil.replace(metadesc, "{0}",
                            _linktext);

                    writer.write(metadesc + lineSign);

                    continue;
                }

                int start4 = line.indexOf("#metakeywords");

                if (start4 >= 0) {
                    metakeywords = StringUtil.replace(metakeywords, "{0}",
                            _keyword);

                    writer.write(metakeywords + lineSign);

                    continue;
                }

                writer.write(line + lineSign);
            }

            writer.flush();
            writer.close();
            reader.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return _dd;
    }
}
